home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / macostools.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  6.1 KB  |  149 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. """macostools - Various utility functions for MacOS.
  5.  
  6. mkalias(src, dst) - Create a finder alias 'dst' pointing to 'src'
  7. copy(src, dst) - Full copy of 'src' to 'dst'
  8. """
  9. from Carbon import Res
  10. from Carbon import File, Files
  11. import os
  12. import sys
  13. import MacOS
  14. import time
  15.  
  16. try:
  17.     openrf = MacOS.openrf
  18. except AttributeError:
  19.     openrf = open
  20.  
  21. Error = 'macostools.Error'
  22. BUFSIZ = 524288
  23. COPY_FLAGS = Files.kIsStationary | Files.kNameLocked | Files.kHasBundle | Files.kIsInvisible | Files.kIsAlias
  24.  
  25. def mkalias(src, dst, relative = None):
  26.     '''Create a finder alias'''
  27.     srcfsr = File.FSRef(src)
  28.     (dstdir, dstname) = os.path.split(dst)
  29.     if not dstdir:
  30.         dstdir = os.curdir
  31.     
  32.     dstdirfsr = File.FSRef(dstdir)
  33.     if relative:
  34.         relativefsr = File.FSRef(relative)
  35.         alias = File.FSNewAlias(relativefsr, srcfsr)
  36.     else:
  37.         alias = srcfsr.FSNewAliasMinimal()
  38.     (dstfsr, dstfss) = Res.FSCreateResourceFile(dstdirfsr, unicode(dstname), File.FSGetResourceForkName())
  39.     h = Res.FSOpenResourceFile(dstfsr, File.FSGetResourceForkName(), 3)
  40.     resource = Res.Resource(alias.data)
  41.     resource.AddResource('alis', 0, '')
  42.     Res.CloseResFile(h)
  43.     dstfinfo = dstfss.FSpGetFInfo()
  44.     dstfinfo.Flags = dstfinfo.Flags | 32768
  45.     dstfss.FSpSetFInfo(dstfinfo)
  46.  
  47.  
  48. def mkdirs(dst):
  49.     """Make directories leading to 'dst' if they don't exist yet"""
  50.     if dst == '' or os.path.exists(dst):
  51.         return None
  52.     
  53.     (head, tail) = os.path.split(dst)
  54.     if os.sep == ':' and not (':' in head):
  55.         head = head + ':'
  56.     
  57.     mkdirs(head)
  58.     os.mkdir(dst, 511)
  59.  
  60.  
  61. def touched(dst):
  62.     '''Tell the finder a file has changed. No-op on MacOSX.'''
  63.     if sys.platform != 'mac':
  64.         return None
  65.     
  66.     import warnings
  67.     warnings.filterwarnings('ignore', 'macfs.*', DeprecationWarning, __name__)
  68.     import macfs
  69.     file_fss = macfs.FSSpec(dst)
  70.     (vRefNum, dirID, name) = file_fss.as_tuple()
  71.     dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
  72.     (crdate, moddate, bkdate) = dir_fss.GetDates()
  73.     now = time.time()
  74.     if now == moddate:
  75.         now = now + 1
  76.     
  77.     
  78.     try:
  79.         dir_fss.SetDates(crdate, now, bkdate)
  80.     except macfs.error:
  81.         pass
  82.  
  83.  
  84.  
  85. def touched_ae(dst):
  86.     '''Tell the finder a file has changed'''
  87.     pardir = os.path.split(dst)[0]
  88.     if not pardir:
  89.         pardir = os.curdir
  90.     
  91.     import Finder
  92.     f = Finder.Finder()
  93.     f.update(File.FSRef(pardir))
  94.  
  95.  
  96. def copy(src, dst, createpath = 0, copydates = 1, forcetype = None):
  97.     '''Copy a file, including finder info, resource fork, etc'''
  98.     src = File.pathname(src)
  99.     dst = File.pathname(dst)
  100.     if createpath:
  101.         mkdirs(os.path.split(dst)[0])
  102.     
  103.     ifp = open(src, 'rb')
  104.     ofp = open(dst, 'wb')
  105.     d = ifp.read(BUFSIZ)
  106.     while d:
  107.         ofp.write(d)
  108.         d = ifp.read(BUFSIZ)
  109.     ifp.close()
  110.     ofp.close()
  111.     ifp = openrf(src, '*rb')
  112.     ofp = openrf(dst, '*wb')
  113.     d = ifp.read(BUFSIZ)
  114.     while d:
  115.         ofp.write(d)
  116.         d = ifp.read(BUFSIZ)
  117.     ifp.close()
  118.     ofp.close()
  119.     srcfss = File.FSSpec(src)
  120.     dstfss = File.FSSpec(dst)
  121.     sf = srcfss.FSpGetFInfo()
  122.     df = dstfss.FSpGetFInfo()
  123.     (df.Creator, df.Type) = (sf.Creator, sf.Type)
  124.     if forcetype != None:
  125.         df.Type = forcetype
  126.     
  127.     df.Flags = sf.Flags & COPY_FLAGS
  128.     dstfss.FSpSetFInfo(df)
  129.     if copydates:
  130.         srcfsr = File.FSRef(src)
  131.         dstfsr = File.FSRef(dst)
  132.         (catinfo, _, _, _) = srcfsr.FSGetCatalogInfo(Files.kFSCatInfoAllDates)
  133.         dstfsr.FSSetCatalogInfo(Files.kFSCatInfoAllDates, catinfo)
  134.     
  135.     touched(dstfss)
  136.  
  137.  
  138. def copytree(src, dst, copydates = 1):
  139.     '''Copy a complete file tree to a new destination'''
  140.     if os.path.isdir(src):
  141.         mkdirs(dst)
  142.         files = os.listdir(src)
  143.         for f in files:
  144.             copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
  145.         
  146.     else:
  147.         copy(src, dst, 1, copydates)
  148.  
  149.